package cn.dyp.com.authoritymanagement.adapter;

import java.util.List;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import cn.dyp.com.authoritymanagement.R;
import cn.dyp.com.authoritymanagement.entity.User;

/**
 * 自定义适配器类 适配Spinner
 * 
 * @author asus
 *
 */
public class MyAdapter extends BaseAdapter {
	private List<User> mList;
	private LayoutInflater mInflater;

	/**
	 * 构造函数
	 * 
	 * @param context
	 *            上下文对象
	 * @param objects
	 *            数据源
	 */
	public MyAdapter(Context context, List<User> objects) {
		this.mList = objects;
		this.mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	/**
	 * 获取单个Item的数据源
	 * 
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public User getItem(int position) {
		return mList.get(position);
	}

	/**
	 * 获取数据源长度
	 * 
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		return mList.size();
	}

	/**
	 * 获取单个Item的位置
	 * 
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int position) {
		return position;
	}

	/**
	 * 获取自定义View
	 * 
	 * @see android.widget.Adapter#getView(int, View,
	 *      ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item, parent, false);
			holder = new ViewHolder();
			holder.mUserTV = (TextView) convertView.findViewById(R.id.user);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.mUserTV.setText(mList.get(position).getName());
		return convertView;
	}

	/**
	 * 视图封装类
	 * 
	 * @author asus
	 *
	 */
	class ViewHolder {
		TextView mUserTV;
	}

}
